<template>
  <div class="dashboard-container">
    <!-- 欢迎信息 -->
    <div class="welcome-section">
      <h3>欢迎回来，{{ adminInfo.username || '管理员' }}</h3>
      <p class="current-date">{{ currentDate }}</p>
    </div>

    <!-- 数据统计卡片 -->
    <div class="stats-cards">
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon">
            <i class="el-icon-s-custom"></i>
          </div>
          <div class="stat-info">
            <div class="stat-number">2,546</div>
            <div class="stat-label">注册用户</div>
          </div>
          <div class="stat-change">
            <span class="increase">+5.2%</span>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon">
            <i class="el-icon-tickets"></i>
          </div>
          <div class="stat-info">
            <div class="stat-number">142</div>
            <div class="stat-label">非遗项目</div>
          </div>
          <div class="stat-change">
            <span class="increase">+2.1%</span>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon">
            <i class="el-icon-date"></i>
          </div>
          <div class="stat-info">
            <div class="stat-number">36</div>
            <div class="stat-label">文化活动</div>
          </div>
          <div class="stat-change">
            <span class="increase">+8.3%</span>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-icon">
            <i class="el-icon-chat-line-square"></i>
          </div>
          <div class="stat-info">
            <div class="stat-number">158</div>
            <div class="stat-label">意见反馈</div>
          </div>
          <div class="stat-change">
            <span class="decrease">-1.5%</span>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 最近活动和通知 -->
    <div class="recent-sections">
      <el-card class="recent-section">
        <template #header>
          <div class="card-header">
            <span>最近活动</span>
            <el-button type="text" size="small" class="view-all">查看全部</el-button>
          </div>
        </template>
        <el-table :data="recentActivities" style="width: 100%">
          <el-table-column prop="name" label="活动名称" width="180"></el-table-column>
          <el-table-column prop="date" label="日期" width="120"></el-table-column>
          <el-table-column prop="location" label="地点" width="180"></el-table-column>
          <el-table-column prop="status" label="状态">
            <template #default="scope">
              <el-tag :type="scope.row.status === '进行中' ? 'success' : 'info'">
                {{ scope.row.status }}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <el-card class="recent-section">
        <template #header>
          <div class="card-header">
            <span>待处理事项</span>
            <el-button type="text" size="small" class="view-all">查看全部</el-button>
          </div>
        </template>
        <div class="todo-list">
          <div v-for="todo in todos" :key="todo.id" class="todo-item">
            <el-checkbox :checked="todo.completed" @change="handleTodoChange(todo.id)">
              <span class="todo-text">{{ todo.text }}</span>
            </el-checkbox>
            <div class="todo-time">{{ todo.time }}</div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getAdminInfo } from '../../api/auth'

const adminInfo = ref({})
const currentDate = ref('')

// 模拟数据
const recentActivities = ref([
  {
    name: '非遗文化展览',
    date: '2023-11-15',
    location: '文化中心',
    status: '进行中'
  },
  {
    name: '传统手工艺体验',
    date: '2023-11-18',
    location: '市民广场',
    status: '进行中'
  },
  {
    name: '非遗传承人讲座',
    date: '2023-11-20',
    location: '图书馆',
    status: '未开始'
  },
  {
    name: '传统音乐表演',
    date: '2023-11-25',
    location: '音乐厅',
    status: '未开始'
  }
])

const todos = ref([
  {
    id: 1,
    text: '审核用户上传的非遗项目',
    completed: false,
    time: '2023-11-15'
  },
  {
    id: 2,
    text: '处理用户反馈意见',
    completed: false,
    time: '2023-11-14'
  },
  {
    id: 3,
    text: '更新系统公告',
    completed: true,
    time: '2023-11-12'
  },
  {
    id: 4,
    text: '准备下个月活动计划',
    completed: false,
    time: '2023-11-16'
  }
])

// 获取管理员信息
const fetchAdminInfo = async () => {
  try {
    const response = await getAdminInfo()
    if (response.success === true || response.errorCode === 20000) {
      if (response.data) {
        adminInfo.value = response.data
      }
    }
  } catch (error) {
    console.error('获取管理员信息失败:', error)
  }
}

// 更新日期
const updateDate = () => {
  const now = new Date()
  const options = {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    weekday: 'long'
  }
  currentDate.value = now.toLocaleDateString('zh-CN', options)
}

// 处理待办事项变更
const handleTodoChange = (todoId) => {
  const todo = todos.value.find(item => item.id === todoId)
  if (todo) {
    todo.completed = !todo.completed
  }
}

onMounted(() => {
  fetchAdminInfo()
  updateDate()
})
</script>

<style scoped>
.dashboard-container {
  width: 100%;
}

/* 欢迎区域 */
.welcome-section {
  margin-bottom: 20px;
}

.welcome-section h3 {
  margin: 0 0 5px 0;
  color: #2c3e50;
  font-size: 20px;
}

.current-date {
  color: #7f8c8d;
  margin: 0;
  font-size: 14px;
}

/* 统计卡片 */
.stats-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.stat-card {
  height: 100%;
  transition: transform 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-5px);
}

.stat-content {
  display: flex;
  align-items: center;
  padding: 10px 0;
}

.stat-icon {
  width: 48px;
  height: 48px;
  background-color: #e8f4fd;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  font-size: 24px;
  color: #1a5276;
}

.stat-info {
  flex: 1;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  color: #2c3e50;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 14px;
  color: #7f8c8d;
}

.stat-change {
  font-size: 14px;
}

.increase {
  color: #27ae60;
}

.decrease {
  color: #e74c3c;
}

/* 最近活动和待办事项 */
.recent-sections {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 20px;
}

.recent-section {
  height: 100%;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.view-all {
  color: #1a5276;
}

/* 待办事项列表 */
.todo-list {
  max-height: 300px;
  overflow-y: auto;
}

.todo-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #f0f0f0;
}

.todo-item:last-child {
  border-bottom: none;
}

.todo-text {
  font-size: 14px;
  color: #333;
}

.todo-time {
  font-size: 12px;
  color: #999;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .stats-cards {
    grid-template-columns: 1fr;
  }
  
  .recent-sections {
    grid-template-columns: 1fr;
  }
}
</style>